package com.starsea.book.ui.newpage.adapter

import android.content.Context
import android.view.ViewGroup
import com.starsea.book.base.adapter.ItemViewHolder
import com.starsea.book.base.adapter.RecyclerAdapter
import com.starsea.book.databinding.ItemBookCommentAllParentBinding
import com.starsea.book.ui.newpage.entity.CommentEntity
import com.starsea.book.ui.newpage.utils.loadRoundImage
import com.starsea.book.ui.widget.recycler.ItemTouchCallback
import com.starsea.book.utils.gone
import com.starsea.book.utils.visible

class CommentAllAdapter(
    var mContext: Context,
    var type: Int = 0,
    var onChanged: ((value: CommentEntity, type: Int, position: Int) -> Unit)? = null
) :
    RecyclerAdapter<CommentEntity, ItemBookCommentAllParentBinding>(mContext),
    ItemTouchCallback.Callback {


    override fun getViewBinding(parent: ViewGroup): ItemBookCommentAllParentBinding {
        return ItemBookCommentAllParentBinding.inflate(inflater, parent, false)
    }

    override fun registerListener(
        holder: ItemViewHolder,
        binding: ItemBookCommentAllParentBinding
    ) {
        binding.root.setOnClickListener {
            getItem(holder.layoutPosition - 1)?.let {
                onChanged!!.invoke(it, 1, holder.layoutPosition - 1)
            }

        }
        binding.rtvReply.setOnClickListener {
            getItem(holder.layoutPosition - 1)?.let {
                onChanged!!.invoke(it, 1, holder.layoutPosition - 1)
            }

        }
        binding.rllCommentChild.setOnClickListener {
            getItem(holder.layoutPosition - 1)?.let {
                onChanged!!.invoke(it, 2, holder.layoutPosition - 1)
            }

        }
        binding.rtvPrise.setOnClickListener {
            getItem(holder.layoutPosition - 1)?.let {
                onChanged!!.invoke(it, 3, holder.layoutPosition - 1)
            }

        }
    }

    override fun convert(
        holder: ItemViewHolder,
        binding: ItemBookCommentAllParentBinding,
        item: CommentEntity,
        payloads: MutableList<Any>
    ) {
        binding.run {
            ivHeader.loadRoundImage(mContext, item.headUrl ?: "")
            tvNickName.text = item.custName ?: ""
            tvCommentTime.text = item.createTime ?: ""
            tvCommentContent.text = item.commentDesc ?: ""
//            Singleton.getTimeMins(item.createTime)
            rtvPrise.text = if (item.likeCoount > 99) "99+" else item.likeCoount.toString()
            tvCommentReadTime.text = "认为本书好看"
            rbRating.rating = item.scoreNumber / 2
            rtvReply.visible(type == 0)

            rbRating.visible(type == 0)
            tvCommentReadTime.visible(type == 0)
            rtvPrise.isSelected = item.likeFlag == 1


            if (item.replyCount == 0) {
                rllCommentChild.gone()
            } else {
                rllCommentChild.visible()
                tvCommentChild.text = "全部${item.replyCount}条评论"
            }
        }
    }


}